package com.paipai.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paipai.entity.Bidding;
import com.paipai.jwtproperty.JwtProperties;
import com.paipai.service.BiddingService;
import com.paipai.utils.BaseController;
import com.paipai.utils.CookieUtils;
import com.paipai.utils.JwtUtils;
import com.paipai.utils.UserInfo;


@Controller
@RequestMapping("/Bidding")
public class BiddingController extends BaseController {
	@Autowired
	private BiddingService biddingService;
	
	
	@Autowired
	private JwtProperties jwp;
	
	
	
	/**
	 * 获取商品 的竞价信息
	 * 
	 * @param date
	 */
	@ResponseBody
	@RequestMapping("/searchBidding")
	public Object searchBidding(@RequestParam("id") Integer id) {
		List<Bidding> list = biddingService.ProductBidding(id);
		System.out.println(list);
		return list;

	}
	
	
	
	
	/**
	 * 用户出价
	 * 
	 * @param date
	 */
	@ResponseBody
	@RequestMapping("/executeBidding")
	public Object executeBidding(Integer proId,Float offerPrice ,HttpServletRequest req) {
		//HttpSession session = req.getSession();
		//从session获取用户名 id
		String token = CookieUtils.getCookieValue(req, "loginToken");
		
		String flag=null;
		try {
			UserInfo userInfo = JwtUtils.getInfoFromToken(token, jwp.getPublicKey());
			Integer id = Integer.parseInt(userInfo.getId().toString());
			String uname = userInfo.getUsername();
			flag = biddingService.executeBidding(proId, offerPrice, uname, id);
			return flag;
		} catch (Exception e) {
			System.out.println("token解析错误");
			e.printStackTrace();
			return "403";
		}

	

	}
	
	
	
	/**
	 * 获取用户参与的竞价
	 * 
	 * @param 
	 */
	@ResponseBody
	@RequestMapping("/searchbiddingrecord")
	public Object searchbiddingrecord(HttpServletRequest req) {
		
		HttpSession session = req.getSession();
		//从session获取用户名 id
		Integer uid = Integer.parseInt(session.getAttribute("loginUid").toString()) ;
		
		
		List<List<Bidding>> Biddinglist = biddingService.findUserbiddingrecord(uid);
		//System.out.println(Biddinglist);
		for (List<Bidding> list : Biddinglist) {
			//System.out.println(list);
		}
		return Biddinglist;

	}
	
	/**
	 * 定时任务 查看商品是否有竞价记录
	 * 
	 * @param 
	 */
	@ResponseBody
	@RequestMapping("/findEndDatebiddingByProId")
	public Bidding findEndDatebiddingByProId(@RequestParam("id") Integer id) {
		Bidding bidding =biddingService.findEndDatebiddingByProId(id);
		return  bidding;
		
	};

}